<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>RAR</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="wrappers.ssh2.html">« ssh2://</a></li>
      <li style="float: right;"><a href="wrappers.audio.html">ogg:// »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="wrappers.html">支持的协议和封装协议</a></li>
    <li>RAR</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="wrappers.rar" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">rar://</h1>
  <p class="refpurpose"><span class="refname">rar://</span> &mdash; <span class="dc-title">RAR</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-wrappers.rar-description">
  <h3 class="title">说明</h3>
  <p class="para">
   封装协议采用 url 编码，（相对/绝对）路径的 RAR 归档、一个可选的星号（<code class="literal">*</code>）、一个可选的井号（<code class="literal">#</code>
   ）和一个可选的存储在归档中的 url 编码条目名称。指定条目名称必须带有井号，条目名称开头的斜线是可选的。
  </p>
  
  <p class="simpara">
   该封装协议可以打开文件和目录。当打开目录时，星号会强制返回未编码的目录条目名称。如果不指定星号，将返回 URL 编码的目录条目名称 ——
   这样做的原因是存在类似 URL 编码的文件名时，允许封装协议能够与内置方法（比如 
   <span class="classname"><a href="class.recursivedirectoryiterator.html" class="classname">RecursiveDirectoryIterator</a></span>）一起正确使用。
  </p>
  
  <p class="simpara">
   如果不包含井号和条目名称部分，将会显示归档的根目录。与常规目录不同的是生成的流不会包含诸如修改时间之类的信息，
   因为根目录不会存储在归档的条目中。当访问根目录时， <span class="classname"><a href="class.recursivedirectoryiterator.html" class="classname">RecursiveDirectoryIterator</a></span>
   与封装协议一起使用时需要在 ULR 中包含井号，以便正确构建子级 URL。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <strong>该封装协议不会默认启用</strong><br />
   <span class="simpara">
    为了使用 <var class="filename">rar://</var> 封装协议，必须从 <a href="https://pecl.php.net/" class="link external">&raquo;&nbsp;PECL</a>
    中安装可用的 <a href="https://pecl.php.net/package/rar" class="link external">&raquo;&nbsp;rar</a> 扩展。
   </span>
  </p></blockquote>
  <p class="simpara">
   <var class="filename">rar://</var>
   自 PECL rar 3.0.0 起可用
  </p>

 </div>


 <div class="refsect1 usage" id="refsect1-wrappers.rar-usage"> 
  <h3 class="title">用法</h3>
  <ul class="itemizedlist">
   <li class="listitem"><span class="simpara"><var class="filename">rar://&lt;url encoded archive name&gt;[*][#[&lt;url encoded entry name&gt;]]</var></span></li>
  </ul>
 </div>
 

 <div class="refsect1 options" id="refsect1-wrappers.rar-options">
  <h3 class="title">可选项</h3>
  <p class="para">
   <table class="doctable table">
    <caption><strong>封装协议摘要</strong></caption>
    
     <thead>
      <tr>
       <th>属性</th>
       <th>支持</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>受限于 <a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">allow_url_fopen</a></td>
       <td>No</td>
      </tr>

      <tr>
       <td>受限于 <a href="filesystem.configuration.html#ini.allow-url-include" class="link">allow_url_include</a></td>
       <td>No</td>
      </tr>

      <tr>
       <td>允许读取</td>
       <td>Yes</td>
      </tr>

      <tr>
       <td>允许写入</td>
       <td>No</td>
      </tr>

      <tr>
       <td>允许附加</td>
       <td>No</td>
      </tr>

      <tr>
       <td>允许同时读写</td>
       <td>No</td>
      </tr>

      <tr>
       <td>支持 <span class="function"><a href="function.stat.html" class="function">stat()</a></span></td>
       <td>Yes</td>
      </tr>

      <tr>
       <td>支持 <span class="function"><a href="function.unlink.html" class="function">unlink()</a></span></td>
       <td>No</td>
      </tr>

      <tr>
       <td>支持 <span class="function"><a href="function.rename.html" class="function">rename()</a></span></td>
       <td>No</td>
      </tr>

      <tr>
       <td>支持 <span class="function"><a href="function.mkdir.html" class="function">mkdir()</a></span></td>
       <td>No</td>
      </tr>

      <tr>
       <td>支持 <span class="function"><a href="function.rmdir.html" class="function">rmdir()</a></span></td>
       <td>No</td>
      </tr>

     </tbody>
    
   </table>

  </p>

  
  <p class="para">
   <table class="doctable table">
    <caption><strong>上下文选项</strong></caption>
    
     <thead>
      <tr>
       <th>名称</th>
       <th>用法</th>
       <th>默认</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td><code class="literal">open_password</code></td>
       <td>用于加密归档报头的密码（如果有）。如果存在后者，WinRAR 将使用相同的密码作为报头密码加密所有文件，
       因此对于带有加密报头的归档， <code class="literal">file_password</code> 将会被忽略。
       </td>
       <td class="empty">&nbsp;</td>
      </tr>

      <tr>
       <td><code class="literal">file_password</code></td>
       <td>
        用于加密文件的密码（如果有）。如果报头也加密，这个选项将会被忽略以支持 <code class="literal">open_password</code>。
        有两个选项的原因是为了支持可能会出现的报头和文件密码不同的归档。请注意，如果归档没有加密它的报头，
        <code class="literal">open_password</code> 将会忽略且必须使用此选项代替。
       </td>
       <td class="empty">&nbsp;</td>
      </tr>

      <tr>
       <td><code class="literal">volume_callback</code></td>
       <td>确定缺失卷的路径回调。请参阅 <span class="methodname"><a href="rararchive.open.html" class="methodname">RarArchive::open()</a></span> 获取更多信息。
       </td>
       <td class="empty">&nbsp;</td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>
 

 <div class="refsect1 examples" id="refsect1-wrappers.rar-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-427">
   <p><strong>示例 #1 遍历 RAR 归档</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MyRecDirIt&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">RecursiveDirectoryIterator&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">current</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">rawurldecode</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getSubPathName</span><span style="color: #007700">())&nbsp;.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(</span><span style="color: #0000BB">is_dir</span><span style="color: #007700">(</span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">current</span><span style="color: #007700">())?</span><span style="color: #DD0000">"&nbsp;[DIR]"</span><span style="color: #007700">:</span><span style="color: #DD0000">""</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$f&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"rar://"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">rawurlencode</span><span style="color: #007700">(</span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #0000BB">__FILE__</span><span style="color: #007700">))&nbsp;.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">DIRECTORY_SEPARATOR&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'dirs_and_extra_headers.rar#'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$it&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">RecursiveTreeIterator</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">MyRecDirIt</span><span style="color: #007700">(</span><span style="color: #0000BB">$f</span><span style="color: #007700">));<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$it&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$s</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$s</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
|-allow_everyone_ni [DIR]
|-file1.txt
|-file2_אּ.txt
|-with_streams.txt
\-אּ [DIR]
  |-אּ\%2Fempty%2E [DIR]
  | \-אּ\%2Fempty%2E\file7.txt
  |-אּ\empty [DIR]
  |-אּ\file3.txt
  |-אּ\file4_אּ.txt
  \-אּ\אּ_2 [DIR]
    |-אּ\אּ_2\file5.txt
    \-אּ\אּ_2\file6_אּ.txt
</pre></div>
   </div>
  </div>
  <div class="example" id="example-428">
   <p><strong>示例 #2 打开加密文件（报头加密）</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$stream&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"rar://"&nbsp;</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">rawurlencode</span><span style="color: #007700">(</span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #0000BB">__FILE__</span><span style="color: #007700">))&nbsp;.&nbsp;</span><span style="color: #0000BB">DIRECTORY_SEPARATOR&nbsp;</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'encrypted_headers.rar'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'#encfile1.txt'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">stream_context_create</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'rar'&nbsp;</span><span style="color: #007700">=&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'open_password'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'samplepassword'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />&nbsp;&nbsp;&nbsp;&nbsp;);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">stream_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">));<br /></span><span style="color: #FF8000">/*&nbsp;在&nbsp;WinRAR&nbsp;中创建日期和最后访问日期是可选的，<br />&nbsp;*&nbsp;因此大多数文件都没有&nbsp;*/<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">fstat</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(26) &quot;Encrypted file 1 contents.&quot;
Array
(
    [0] =&gt; 0
    [1] =&gt; 0
    [2] =&gt; 33206
    [3] =&gt; 1
    [4] =&gt; 0
    [5] =&gt; 0
    [6] =&gt; 0
    [7] =&gt; 26
    [8] =&gt; 0
    [9] =&gt; 1259550052
    [10] =&gt; 0
    [11] =&gt; -1
    [12] =&gt; -1
    [dev] =&gt; 0
    [ino] =&gt; 0
    [mode] =&gt; 33206
    [nlink] =&gt; 1
    [uid] =&gt; 0
    [gid] =&gt; 0
    [rdev] =&gt; 0
    [size] =&gt; 26
    [atime] =&gt; 0
    [mtime] =&gt; 1259550052
    [ctime] =&gt; 0
    [blksize] =&gt; -1
    [blocks] =&gt; -1
)
</pre></div>
   </div>
  </div>
 </div>


</div></div></div></body></html>